//
//  InfoView.m
//  ACSProduct
//
//  Created by preet dhillon on 07/04/12.
//  Copyright (c) 2012 dhillon. All rights reserved.
//

#import "InfoView.h"

@implementation InfoView
@synthesize lableDistance,lableCost,tableDesc,delagate,scrollImage,dictInfo,arraySecTitle,ViewDot,pgCtrl;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
    }
    return self;
}
-(void)setAllImage:(NSArray *)arrayImages
{
    
    self.scrollImage.pagingEnabled = YES;
    int incX = 0;
    [self.scrollImage setContentSize:CGSizeMake([arrayImages count]*320, self.scrollImage.frame.size.height)];
    
    
    for (int i=0; i<[arrayImages  count]; i++)
    {
        VenDealImageView *imagVen = [[VenDealImageView alloc] initWithFrame:CGRectMake(incX, 0, 320, self.scrollImage.frame.size.height)];
        //        imagVen.placeholderImage    = [UIImage imageNamed:@"PlaceHolder.png"];
        //        NSLog(@">>>>> %@",[[arrayImages objectAtIndex:i]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]);
        [imagVen setviewWithUrl:[NSURL URLWithString:[arrayImages objectAtIndex:i]]];
        
        //        imagVen.imageURL =[NSURL URLWithString:[arrayImages objectAtIndex:i]]; //[NSURL URLWithString:[NSString stringWithFormat:IMAGEURL,[[arrayImages objectAtIndex:i]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
        [self.scrollImage addSubview:imagVen];
        incX += 320;
        [imagVen  release];
    }
    
}

-(void)setContentInView:(NSMutableDictionary *)dict
{
    arraySecTitle = [[NSMutableArray alloc] initWithObjects:@"Description",@"Detail", nil];
    self.dictInfo = [[NSMutableDictionary alloc] initWithDictionary:dict];
    self.lableCost = [dict objectForKey:FIELDCOST];
    
    [self.lableDistance setText:[NSString stringWithFormat:@"%@",[ModalController  calDistancebetWithLat:[locationUser.strUserLat doubleValue] with:[locationUser.strUserLong doubleValue] with:[[dict objectForKey:@"Lat"]doubleValue] with:[[dict  objectForKey:@"Long"]doubleValue]]]];
    tableDesc.delegate = self;
    tableDesc.dataSource = self;
    
    
    [tableDesc  reloadData];
}

-(void)setPageControl
{
    //    CGRect frameContrl = [self.pgCtrl  frame];
    //    [self.pgCtrl removeFromSuperview];
    //    
    //    self.pgCtrl = [[UIPageControl  alloc] initWithFrame:frameContrl];
    //    [self addSubview:self.pgCtrl];
    //    
    self.pgCtrl.currentPage = 0;
    self.pgCtrl.numberOfPages = [arrayPgCtrl count];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
	// The number of sections is based on the number of items in the data property list.
	return [self.arraySecTitle count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [self.arraySecTitle objectAtIndex:section];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	
    if(section == 0)
        return 1;	
    if(section == 1)
    {
        if([[dictInfo objectForKey:FIELDEMAIL] length]>0 && [[dictInfo objectForKey:FIELDPHONE] length]>0 && [[dictInfo objectForKey:FIELDURL] length]>0)
            return 6;
        else
            return 3;
    }
    
    //    else
    //        return 5;
    return 0;
	
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 0)
    {
        UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:14.0];
        CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
        CGSize labelSize = [[self.dictInfo  objectForKey:FIELDDESC] sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
        return labelSize.height + 5;
    }
    else
        return 32;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{	    
    
	// Retrieve or create a cell
	UITableViewCellStyle style =  UITableViewCellStyleDefault;
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
    if (cell ==  nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:style reuseIdentifier:@"BaseCell"] autorelease];
        
        if(indexPath.row != 0)
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
        
        if(indexPath.section == 1)
        {
            cell.textLabel.font = [UIFont systemFontOfSize:12];
            switch (indexPath.row) {
                case 0:
                    cell.textLabel.text = [dictInfo objectForKey:FIELDNAME];
                    break;
                case 1:
                    cell.textLabel.text = cell.textLabel.text = [NSString stringWithFormat:@"%@, %@",[dictInfo objectForKey:FIELDADD],[dictInfo objectForKey:FIELDPOSTCODE]];;
                    break;
                case 2:
                {
                    if([dictInfo objectForKey:FIELDOPERATINGHRS])
                        cell.textLabel.text = [NSString stringWithFormat:@"Operating Hours : %@",[dictInfo objectForKey:FIELDOPERATINGHRS]];
                    else
                    {
                         cell.textLabel.text = [NSString stringWithFormat:@"Operating Hours : N.A"];
                    }
                }
                    break;
                case 3:
                    cell.textLabel.text = [dictInfo objectForKey:FIELDPHONE];
                    break;
                case 4:
                    cell.textLabel.text = [dictInfo objectForKey:FIELDURL];
                    break;
                case 5:
                    cell.textLabel.text = [dictInfo objectForKey:FIELDEMAIL];
                    break;
//                case 6:
//                    cell.textLabel.text = [dictInfo objectForKey:FIELDURL];
//                    break;
                default:
                    break;
            }
        }
        else
        {
            cell.textLabel.text = [self.dictInfo  objectForKey:FIELDDESC];
            cell.textLabel.font=[UIFont fontWithName:@"Helvetica" size:14.0];
            cell.textLabel.numberOfLines=0;
            cell.textLabel.lineBreakMode=UILineBreakModeWordWrap;
            // [cell.textLabel setAdjustsFontSizeToFitWidth:YES];
        }
        
    }
    
    
    //	ListCell *cell = (ListCell *)[tableView dequeueReusableCellWithIdentifier:@"ListCell"];
    //	if (!cell) 
    //	{
    //		cell = [[[NSBundle mainBundle] loadNibNamed:@"ListCell" owner:self options:nil] lastObject];
    //		cell.backgroundColor=[UIColor whiteColor];
    //		cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    //	}
    //	
    //    
    //    cell.costLabel.text = [NSString stringWithFormat:@"$%@",[[arrayList objectAtIndex:indexPath.row] objectForKey:FIELDCOST]];
    //    cell.venueNameLabel.text = [[arrayList objectAtIndex:indexPath.row] objectForKey:FIELDNAME];
    //    cell.dealLabel.text = [[arrayList objectAtIndex:indexPath.row] objectForKey:FIELDDEAL];
    //    cell.distanceLabel.text = @" ";
    //    
    //    cell.venueImage.image = [(UIImageView*)[arrayImages objectAtIndex:indexPath.row] image];
    return (UITableViewCell *)cell;
	
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    //    self.pgCtrl.currentPage = 0;
    if(indexPath.section == 1)
        switch (indexPath.row) {
            case 1:
            {
//                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Info" 
//                                                                message:MAP_VISIT 
//                                                               delegate:self 
//                                                      cancelButtonTitle:nil
//                                                      otherButtonTitles:@"NO",@"YES", nil];
//                alert.tag = 1;
//                [alert show];
//                [alert release];
                break;
            }
            case 3:
            {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Info" 
                                                                message:DIAL_A_NUMBER 
                                                               delegate:self 
                                                      cancelButtonTitle:nil
                                                      otherButtonTitles:@"NO",@"YES", nil];
                alert.tag = 3;
                [alert show];
                [alert release];
                break;
            }
            case 4:
            {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Info" 
                                                                message:WEBLINK_VISIT 
                                                               delegate:self 
                                                      cancelButtonTitle:nil
                                                      otherButtonTitles:@"NO",@"YES", nil];
                alert.tag = 4;
                [alert show];
                [alert release];
            }
                break;
            case 5:
            {
                [delagate  openEmail:[dictInfo objectForKey:FIELDEMAIL]];
            }
                break;
                
            default:
                break;
        }
}



/*
 UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:15.0];
 CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
 labelSize = [self.stringDescSection4 sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
 
 return labelSize.height + 50;
 
 
 
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect
 {
 // Drawing code
 }
 */

#pragma mark- UIScrollview Delegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    //    self.pgCtrl.numberOfPages = [arrayPgCtrl count];
    CGFloat pageWidth = self.scrollImage.frame.size.width;
    float fractionalPage = self.scrollImage.contentOffset.x / pageWidth;
    NSInteger page = lround(fractionalPage);
    self.pgCtrl.currentPage = page; 
}


- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    
    if(buttonIndex == 1)
    {
        if(LOGOFF == 1) NSLog(@"stringURL  = %d",alertView.tag);
        switch (alertView.tag) {
            case 1:
            {
                NSString *stringURL = [NSString stringWithFormat:LINKMAPADD,[[dictInfo objectForKey:FIELDADD]stringByReplacingOccurrencesOfString:@" " withString:@"+"]];
                NSLog(@"%@",stringURL);
                NSURL *url = [NSURL URLWithString:stringURL];
                [[UIApplication sharedApplication] openURL:url];
                break;
            }  
            case 3:
            {
                NSString *stringURL = [NSString stringWithFormat:@"tel:%@",[dictInfo objectForKey:FIELDPHONE]];
                NSLog(@"%@",stringURL);
                NSURL *url = [NSURL URLWithString:stringURL];
                [[UIApplication sharedApplication] openURL:url];
                break;
            }    
            case 4:
            {
                NSString *stringURL = [dictInfo objectForKey:FIELDURL];
                if(LOGOFF == 1) NSLog(@"stringURL %@",stringURL);
                NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://%@",stringURL]];
                [[UIApplication sharedApplication] openURL:url];            
                break;
            }
            default:
                break;
        }
    }
}


@end
